package com.tensai.study.基础.数据结构.栈;


import java.util.List;
import java.util.Stack;

/**
 * Stack的测试程序。测试常用API的用法
 *
 * @author Tensai
 */
public class StackTest {

    public static void main(String[] args) {
        Stack<Integer> stack = new Stack<>();
        // 将1,2,3,4,5添加到栈中
        for (int i = 1; i < 6; i++) {
            stack.push(i);
        }

        /*// 遍历并打印出该栈
        iteratorThroughRandomAccess(stack);

        // 查找“2”在栈中的位置，并输出
        int pos = stack.search("2");
        System.out.println("the postion of 2 is:" + pos);

        // pup栈顶元素之后，遍历栈
        stack.pop();
        iteratorThroughRandomAccess(stack);

        // peek栈顶元素之后，遍历栈
        String val = (String) stack.peek();
        System.out.println("peek:" + val);
        iteratorThroughRandomAccess(stack);

        // 通过Iterator去遍历Stack
        iteratorThroughIterator(stack);*/

        while (!stack.empty()) {
            System.out.println(stack.pop());
        }
    }

    /**
     * 通过快速访问遍历Stack
     */
    public static void iteratorThroughRandomAccess(List list) {
        String val;
        for (Object o : list) {
            val = (String) o;
            System.out.print(val + " ");
        }
        System.out.println();
    }

    public static void iteratorThroughIterator(List list) {

        String val;
        for (Object o : list) {
            val = (String) o;
            System.out.print(val + " ");
        }
        System.out.println();
    }

}

